﻿using System;
using System.Collections.Specialized;
using System.Web.UI;
using AjaxControlToolkit.HTMLEditor;
using NotAClue.ComponentModel.DataAnnotations;
using NotAClue.Web.DynamicData;
using System.Drawing;

namespace DD_Extensions
{
    public partial class Html_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // set field options
            this.SetFieldOptions();

            // add CSS from meta data
            this.ApplyCss();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var htmlAttr = Column.GetAttributeOrDefault<HtmlEditorAttribute>();
            if (htmlAttr.ActiveMode != ActiveModeType.Design)
                Editor1.ActiveMode = htmlAttr.ActiveMode;

            if (htmlAttr.AutoFocus)
                Editor1.AutoFocus = true;

            if (htmlAttr.CssClass != null)
                Editor1.CssClass = htmlAttr.CssClass;

            if (htmlAttr.DesignPanelCssPath != null)
                Editor1.DesignPanelCssPath = htmlAttr.DesignPanelCssPath;

            if (htmlAttr.DocumentCssPath != null)
                Editor1.DocumentCssPath = htmlAttr.DocumentCssPath;

            if (htmlAttr.Height > 0)
                Editor1.Height = htmlAttr.Height;

            if (htmlAttr.HtmlPanelCssClass != null)
                Editor1.HtmlPanelCssClass = htmlAttr.HtmlPanelCssClass;

            if (htmlAttr.IgnoreTab)
                Editor1.IgnoreTab = true;

            if (htmlAttr.InitialCleanUp)
                Editor1.InitialCleanUp = true;

            if (htmlAttr.NoScript)
                Editor1.NoScript = true;

            if (htmlAttr.NoUnicode)
                Editor1.NoUnicode = true;

            if (htmlAttr.OnClientActiveModeChanged != null)
                Editor1.OnClientActiveModeChanged = htmlAttr.OnClientActiveModeChanged;

            if (htmlAttr.OnClientBeforeActiveModeChanged != null)
                Editor1.OnClientBeforeActiveModeChanged = htmlAttr.OnClientBeforeActiveModeChanged;

            if (htmlAttr.SuppressTabInDesignMode)
                Editor1.SuppressTabInDesignMode = true;

            if (htmlAttr.Width > 0)
                Editor1.Width = htmlAttr.Width;

            SetUpValidator(RequiredFieldValidator1);
            RequiredFieldValidator1.Text = "*";
            SetUpValidator(RegularExpressionValidator1);
            RegularExpressionValidator1.Text = "*";
            SetUpValidator(DynamicValidator1);
            DynamicValidator1.Text = "*";

            // set validators to use image as error indicator
            this.SetValidatorsToImage();

            if (RequiredFieldValidator1.Enabled)
                Editor1.BackColor = Color.FromArgb(0xFFFEBD);
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(Editor1.Content);
        }

        public override Control DataControl
        {
            get
            {
                return Editor1;
            }
        }
    }
}